package com.sex.bull.melancholy.service.outside;

import com.sex.bull.melancholy.service.mybatis.bean.Oauth2App;
import com.sex.bull.melancholy.service.mybatis.service.Oauth2AppService;
import com.sex.bull.melancholy.service.service.RedisHashUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created by LiM on 2017/8/28.
 */
@Service
public class OauthVerifyService {

    @Autowired
    RedisHashUtil redisHashUtil;
    @Autowired
    Oauth2AppService oauth2AppService;

    private int validTime = 7200;  //有效时间(秒)
    private int refreshTime = 300; //剩余执行刷新时间(秒)
    private String authCodeKey = "authCode";           //code
    private String authCodeTime = "authCodeTime";      //code time
    private String accessTokenKey = "accessToken";     //token
    private String accessTokenTime = "accessTokenTime";//token time

    /**
     * 添加code
     * @param sLinkId
     * @param sAuthCode
     */
    private void addAuthCode(String sLinkId, String sAuthCode){
        redisHashUtil.put(authCodeKey, sLinkId, sAuthCode, validTime);
        redisHashUtil.put(authCodeTime, sLinkId, String.valueOf(System.currentTimeMillis()), validTime);
    }

    /**
     * 添加Token
     * @param sLinkId
     * @param sAccessToken
     */
    private void addAccessToken(String sLinkId, String sAccessToken){
        redisHashUtil.put(accessTokenKey, sLinkId, sAccessToken, validTime);
        redisHashUtil.put(accessTokenTime, sLinkId, String.valueOf(System.currentTimeMillis()), validTime);
    }

    /**
     * 检测Code是否需要刷新
     * @param sLinkId
     * @return
     */
    private boolean checkAuthCodeNeedRefresh(String sLinkId){
        return System.currentTimeMillis() - Long.parseLong(redisHashUtil.get(authCodeTime, sLinkId)) > refreshTime * 1000;
    }

    /**
     * 检测Token是否需要刷新
     * @param sLinkId
     * @return
     */
    private boolean checkAccessTokenNeedRefresh(String sLinkId){
        return System.currentTimeMillis() - Long.parseLong(redisHashUtil.get(accessTokenTime, sLinkId)) > refreshTime * 1000;
    }

    /**
     * 检查并刷新code
     * @param sLinkId
     * @param sAuthCode
     * @return
     */
    private String checkAuthCode(String sLinkId, String sAuthCode){
        String sReturn = "";
        if(redisHashUtil.get(authCodeKey, sLinkId).equals(sAuthCode)){
            if(checkAuthCodeNeedRefresh(sLinkId)){
                sReturn = sAuthCode;
            }else {
                sReturn = getAuthCode(sLinkId);
                redisHashUtil.put(authCodeKey, sLinkId, sReturn, validTime);
                redisHashUtil.put(authCodeTime, sLinkId, String.valueOf(System.currentTimeMillis()), validTime);
            }
        }
        return  sReturn;
    }

    /**
     * 检查并刷新Token
     * @param sLinkId
     * @param sAccessToken
     * @return
     */
    private String checkAccessToken(String sLinkId, String sAccessToken){
        String sReturn = "";
        if(redisHashUtil.get(authCodeKey, sLinkId).equals(sAccessToken)){
            if(checkAuthCodeNeedRefresh(sLinkId)){
                sReturn = sAccessToken;
            }else {
                sReturn = getAccessToken(sLinkId);
                redisHashUtil.put(accessTokenKey, sLinkId, sReturn, validTime);
                redisHashUtil.put(accessTokenTime, sLinkId, String.valueOf(System.currentTimeMillis()), validTime);
            }
        }
        return  sReturn;
    }

    /**
     * 判断Code是否包含连接编号Linkid
     * @param sLinkId
     * @return
     */
    private boolean checkHasLinkIdInAuthCode(String sLinkId){
       return redisHashUtil.isEmpty(authCodeKey, sLinkId);
    }

    /**
     * 判断Token是否包含连接编号LinkId
     * @param sLinkId
     * @return
     */
    private boolean checkHasLinkIdInAccessToken(String sLinkId){
        return redisHashUtil.isEmpty(accessTokenKey, sLinkId);
    }

    /**
     * 检查应用是否存在
     * @param sAppId
     * @param sAppSecret
     * @return
     */
    private boolean checkOauth2App(String sAppId, String sAppSecret){
        boolean bReturn = false;
        Oauth2App oauth2App = new Oauth2App();
        oauth2App.setAppId(sAppId);
        oauth2App.setAppSecret(sAppSecret);
        oauth2App.setIsDelete((byte)0);
        try {
            List<Oauth2App> oauth2AppList = oauth2AppService.queryAllList(oauth2App);
            bReturn = oauth2AppList != null && oauth2AppList.size() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return bReturn;
        }
        return bReturn;
    }




    /**
     * 返回超时时间
     * @return
     */
    public int getValidTime(){
        return validTime;
    }

    public String getAuthCode(String sLinkId){
        return "";
    }

    public String getAccessToken(String sLinkId){
        return "";
    }
}
